package com.lfg.leetcode.leetcode100;


import java.util.HashSet;
import java.util.Set;

public class No128longestConsecutive {

    public static void main(String[] args) {
        int ret = longestConsecutive(new int[]{3, 8, 9, 5, 4, 1, 2});
        System.out.println("[" + ret + "]");
    }


    public static int longestConsecutive(int[] nums) {
        Set<Integer> data = new HashSet<Integer>();
        for (int i : nums) {
            data.add(i);
        }

        int ret = 0;
        for (int i : nums) {
            data.remove(i);
            int total = 1;
            int next = i+1;
            int last = i-1;
            while(data.remove(next)){
                total++;
                next++;
            }
            while(data.remove(last)){
                last--;
                total++;
            }
            ret = Math.max(ret, total);
        }
        return ret;
    }

}
